/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.futco.sysconthos.Modelo.Admisiones.Daos;

import org.futco.sysconthos.Modelo.Admisiones.Paciente;
import com.mysql.jdbc.PreparedStatement;
import java.sql.ResultSet;

import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 * 
 * @author randy fernando
 */
public class PacienteDAO implements IDAO<Paciente> {

	private Model model;
	private PreparedStatement psInsert;

	public PacienteDAO() throws SQLException {
		model = Model.getInstance();
	}

	public void insert(Paciente t) throws SQLException {
		if (psInsert == null) {
			String sqlInsert = "INSERT INTO paciente ( codigo, id, nombre, apellido, sexo, fechaNac, ciudad, direccion, tel, email) VALUES (?,?,?,?,?,?,?,?,?,?)";
			psInsert = (PreparedStatement) model.getCon().prepareStatement(
					sqlInsert);
			psInsert.setString(1, t.getCodigo());
			psInsert.setString(2, t.getId());
			psInsert.setString(3, t.getNombre());
			psInsert.setString(4, t.getApellido());
			psInsert.setString(5, t.getSexo());
			psInsert.setString(6, t.getFechaNac());
			psInsert.setString(7, t.getCiudad());
			psInsert.setString(8, t.getDireccion());
			psInsert.setString(9, t.getTel());
			psInsert.setString(10, t.getEmail());
			
			psInsert.executeUpdate();
		}
	}

	public void delete(Paciente t) throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	public List<Paciente> getAllBy(String parametro, String name)
			throws SQLException {
		List lista = new LinkedList<Paciente>();
		java.sql.PreparedStatement pst = null;
		ResultSet rs = null;
		Paciente p = null;

		try {
			pst = model.getCon().prepareStatement(
					"SELECT * FROM paciente WHERE " + parametro + " like ?");
			pst.setString(1, "%" + name.trim() + "%");
			rs = pst.executeQuery();

			while (rs.next()) {
				p = new Paciente();
				p.setCodigo(rs.getString(1));
				p.setId(rs.getString(2));
				p.setNombre(rs.getString(3));
				p.setApellido(rs.getString(4));
				p.setDireccion(rs.getString(5));
				p.setTel(rs.getString(6));
				p.setEmail(rs.getString(7));

				lista.add(p);
			}

		} finally {
			if (pst != null) {
				pst.close();
			}
			if (rs != null) {
				rs.close();
			}
		}
		return lista;
	}

	public Paciente find(String codigo) throws SQLException {
		PreparedStatement pst = null;
		Paciente p = null;
		ResultSet rs = null;
		try {
			if (!model.isConnect()) {
				throw new SQLException(":V");
			}
			pst = (PreparedStatement) model.getCon().prepareStatement("SELECT * FROM paciente WHERE codigo = ?");
			pst.setString(1, codigo.trim());
			rs = pst.executeQuery();
			if (rs.next()) {
				p = load(rs);
			}
		} finally {
			if (rs != null && pst != null) {
				pst.close();
				rs.close();
			}
		}
		return p;
	}

	public Paciente findIdentificacionC(String id) throws SQLException {
		// PreparedStatement pst = null;
		java.sql.PreparedStatement pst = null;
		Paciente p = new Paciente();
		ResultSet rs = null;
		pst = model
				.getCon()
				.prepareStatement(
						"SELECT DISTINCT p.* from cita c,paciente p where c.codPaciente=p.codigo and id=?");
		pst.setString(1, id);
		rs = pst.executeQuery();
		if (rs.next()) {
			p = load(rs);
		}
		if (pst != null) {
			pst.close();
		}
		if (rs != null) {
			rs.close();
		}
		return p;
	}

	public void update(String identifi, Paciente t) throws SQLException {

		String sqlInsert = "UPDATE paciente SET  codigo = ?,nombre = ?, apellido = ?, sexo = ?, "
				+ "fechaNac = ?, ciudad = ?, direccion = ?, tel = ?, email = ? WHERE id = ?";
		psInsert = (PreparedStatement) model.getCon().prepareStatement(
				sqlInsert);

		psInsert.setString(1, t.getCodigo());
		psInsert.setString(2, t.getNombre());
		psInsert.setString(3, t.getApellido());
		psInsert.setString(4, t.getSexo());
		psInsert.setString(5, t.getFechaNac());
		psInsert.setString(6, t.getCiudad());
		psInsert.setString(7, t.getDireccion());
		psInsert.setString(8, t.getTel());
		psInsert.setString(9, t.getEmail());
		psInsert.setString(10, identifi);
		psInsert.executeUpdate();

	}

	public List<Paciente> getAll() throws SQLException {
		Paciente p = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List lista = new LinkedList();

		try {
			String sqlInsert = "Select * from paciente";
			ps = (PreparedStatement) model.getCon().prepareStatement(sqlInsert);
			rs = ps.executeQuery();
			while (rs.next()) {
				p = this.loadPaciente(rs);
				lista.add(p);
			}

		} finally {
			if (rs != null) {
				rs.close();
			}
			if (ps != null) {
				ps.close();
			}
		}
		return lista;
	}

	public List<Paciente> getAllC() throws SQLException {
		Paciente p = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List lista = new LinkedList();

		try {
			String sqlInsert = "SELECT DISTINCT p.* from cita c,paciente p where c.codPaciente=p.codigo";
			ps = (PreparedStatement) model.getCon().prepareStatement(sqlInsert);
			rs = ps.executeQuery();
			while (rs.next()) {
				p = this.loadPaciente(rs);
				lista.add(p);
			}

		} finally {
			if (rs != null) {
				rs.close();
			}
			if (ps != null) {
				ps.close();
			}
		}
		return lista;
	}

	private Paciente loadPaciente(ResultSet rs) throws SQLException {
		Paciente p = new Paciente();

		p.setCodigo(rs.getString("codigo"));
		p.setId(rs.getString("id"));
		p.setNombre(rs.getString("nombre"));
		p.setApellido(rs.getString("apellido"));
		p.setSexo(rs.getString("sexo"));
		p.setFechaNac(rs.getString("fechaNac"));
		p.setCiudad(rs.getString("ciudad"));
		p.setDireccion(rs.getString("direccion"));
		p.setTel(rs.getString("tel"));
		p.setEmail(rs.getString("email"));

		return p;
	}

	public int NumRegistros() throws SQLException {
		int numfil = 0;
		if (model.getCon() == null) {
			throw new SQLException(" no hay conexion ");
		}
		ResultSet rs = null;
		PreparedStatement pst = null;
		try {
			pst = (PreparedStatement) model.getCon().prepareStatement(
					"select count(*) AS  numfil from paciente");
			rs = pst.executeQuery();

			while (rs.next()) {
				numfil = rs.getInt("numfil");
			}
		} finally {
			if (pst != null) {
				pst.close();
			}

		}
		return numfil;
	}

	public int IncrementarCod() throws SQLException {
		int cod = 0;

		if (model.getCon() == null) {
			throw new SQLException(" no hay conexion ");
		}
		ResultSet rs = null;
		PreparedStatement pst = null;
		try {
			pst = (PreparedStatement) model.getCon().prepareStatement(
					"select max(codigo)+1 AS codInt from paciente");
			rs = pst.executeQuery();

			while (rs.next()) {
				cod = rs.getInt("codInt");
			}
		} finally {
			if (pst != null) {
				pst.close();
			}

		}
		return cod;
	}

	public List<Paciente> getAllByC(String parametro, String name)
			throws SQLException {
		List lista = new LinkedList<Paciente>();
		java.sql.PreparedStatement pst = null;
		ResultSet rs = null;
		Paciente p = null;

		try {
			pst = model.getCon().prepareStatement(
					"SELECT DISTINCT p.* from cita c,paciente p where c.codPaciente=p.codigo AND "
							+ parametro + "  like ?");

			pst.setString(1, "%" + name.trim() + "%");
			rs = pst.executeQuery();

			while (rs.next()) {
				p = this.loadPaciente(rs);
				lista.add(p);
			}

		} finally {
			if (pst != null) {
				pst.close();
			}
			if (rs != null) {
				rs.close();
			}
		}
		return lista;
	}

	public Paciente find(Integer id) throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	public static Paciente load(ResultSet rs) throws SQLException {
		Paciente p = new Paciente();

		p.setCodigo(rs.getString(1));
		p.setId(rs.getString(2));
		p.setNombre(rs.getString(3));
		p.setApellido(rs.getString(4));
		p.setSexo(rs.getString(5));
		p.setFechaNac(rs.getString(6));
		p.setCiudad(rs.getString(7));
		p.setDireccion(rs.getString(8));
		p.setTel(rs.getString(9));
		p.setEmail(rs.getString(10));

		return p;
	}

	public Paciente findIdentificacion(String id) throws SQLException {
		java.sql.PreparedStatement pst = null;
		Paciente p = new Paciente();
		ResultSet rs = null;
		pst = model.getCon().prepareStatement("Select * from paciente where  id= ?");
		pst.setString(1, id);
		rs = pst.executeQuery();
		if (rs.next()) {

			p.setCodigo(rs.getString(1));
			p.setId(rs.getString(2));
			p.setNombre(rs.getString(3));
			p.setApellido(rs.getString(4));
			
			p.setSexo(rs.getString(5));
			p.setFechaNac(rs.getString(6));
			p.setCiudad(rs.getString(7));
			p.setDireccion(rs.getString(8));
			p.setTel(rs.getString(9));
			p.setEmail(rs.getString(10));
			
		}
		if (pst != null) {
			pst.close();
		}
		if (rs != null) {
			rs.close();
		}
		return p;
	}

}
